/*
 * SearchResultScreen.java
 *
 * ?<your company here>, 2003-2008
 * Confidential and proprietary.
 */

package com.peakproducts;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import org.json.me.JSONObject;

import com.peakproducts.ConnectionThread;

/**
 * 
 */
class SearchResultScreen extends PeakScreen{
    private ConnectionThread connThread;
    private BasicEditField customName = new BasicEditField("Customer Fullname:","");
    private BasicEditField address = new BasicEditField("Customer Address:","");
    private BasicEditField Contact = new BasicEditField("Service requested:","");
    private BasicEditField status = new BasicEditField("Current Status:","");
    private PeakField more_btn = new PeakField("Show Details");
    private PeakField update_btn = new PeakField("Change Status");
    private SeparatorField separatorField = new SeparatorField();
    private HorizontalFieldManager hfm = new HorizontalFieldManager();
    private PeakField backBtn = new PeakField("Main Menu");
    private JSONObject leadData;
    SearchResultScreen(ConnectionThread ct, JSONObject data) { //,Object resultData
    	leadData = data;
        connThread = ct;
        setTitle("Lead Manager - Search Result");
        try{
        	customName.setEditable(false);
        	address.setEditable(false);
        	Contact.setEditable(false);
        	status.setEditable(false);
        	customName.setText(leadData.getString("fullname"));
        	address.setText(leadData.getString("address"));
        	Contact.setText(leadData.getString("interest_name"));
        	status.setText(leadData.getString("status_name"));
        }catch (Exception e) {
			Status.show("Errors when get lead result.");
		}
        add(customName);
        add(address);
        add(Contact);
        add(status);
        add(separatorField); 
        //add(more_btn);
        //add(update_btn);
        hfm.add(more_btn);
        hfm.add(update_btn);
        hfm.add(backBtn);
        add(hfm);
        //init event
        MoreBtnListener moreBtnEvent = new MoreBtnListener();
        more_btn.setChangeListener(moreBtnEvent);
        backBtn.setChangeListener(moreBtnEvent);
        
        UpdateBtnListener updateBtnEvent = new UpdateBtnListener();
        update_btn.setChangeListener(updateBtnEvent);
    }
    class MoreBtnListener implements FieldChangeListener {
        public void fieldChanged(Field field, int context) {
        	try{
        		if(!((PeakField)field).getLabel().equals("Main Menu"))
            	{
        			delete(separatorField);
	        		delete(hfm);
		    		//delete(update_btn);
		    		//delete(more_btn);
	        		hfm.delete(more_btn);
		    		BasicEditField city = new BasicEditField("City:",leadData.getString("city"));
		    		BasicEditField postCode = new BasicEditField("Postal Code:",leadData.getString("zip"));
		    		BasicEditField province = new BasicEditField("Province:",leadData.getString("state"));
		    		BasicEditField homeNo = new BasicEditField("Home #:",leadData.getString("home"));
		    		BasicEditField businessNo = new BasicEditField("Business #:",leadData.getString("business"));
		    		BasicEditField cellNo = new BasicEditField("Cell #:",leadData.getString("cell"));
		    		BasicEditField btToContact = new BasicEditField("Best Time to Contact:",leadData.getString("besttime"));
		    		BasicEditField nearest = new BasicEditField("nearest:",leadData.getString("nearest"));
		    		BasicEditField shop_at = new BasicEditField("shop_at:",leadData.getString("shop_at"));
		    		city.setEditable(false);
		    		postCode.setEditable(false);
		    		province.setEditable(false);
		    		homeNo.setEditable(false);
		    		businessNo.setEditable(false);
		    		cellNo.setEditable(false);
		    		btToContact.setEditable(false);
		    		nearest.setEditable(false);
		    		shop_at.setEditable(false);
		    		add(city);
		    		add(postCode);
		    		add(province);
		    		add(homeNo);
		    		add(businessNo);
		    		add(cellNo);
		    		add(btToContact);
		    		add(nearest);
		    		add(shop_at);
		    		add(separatorField);
		    		add(hfm);
            	}else{
            		UiApplication.getUiApplication().pushScreen(new LeadSearchScreen(connThread));
            		close();
            	}
        	}catch (Exception e) {
        		Status.show("Errors when display lead result."+e.getMessage());
			}
        }
    }
    class UpdateBtnListener implements FieldChangeListener {
        public void fieldChanged(Field field, int context) {
        	try{
        		UpdateScreen screen = new UpdateScreen(connThread, leadData);
        		UiApplication.getUiApplication().pushScreen(screen);
        		close();
        	}catch (Exception e) {
				// TODO: handle exception
			}
        }
    }
   
} 
